#!/bin/sh
#mapping the nfs folder
#run the command in the folder to upgrade

function pause()
{
	s="pause here, continue? y/n: " 
	read -p "$s" answ
	while [  "$answ" != "y" ]
	do
		read -p "$s" answ
	done
}

function clear()
{
	echo "clear folder $osnfs, $os"
	if [ -d $osnfs ]
	then
		umount $osnfs
		rmdir $osnfs 
	fi

	if [ -d $os ]
	then
		rm $os -rf 
	fi 
}

function quit()
{
	echo -e "-----------------------done------------------------------------------------\n\n"
	if [ "$1" != "" ]
	then
		exit $1
	fi
}
 
function help()
{
	echo "please indiate the command to run:";
	echo "	clear: clear the temp folders"
	echo "	368: upgrade with 368 os"
	echo "	371: upgrade with 371 os"
}

bRunOnPanel=1
if [ "honeywell" !=  $(hostname) ] #on panel
then 
	bRunOnPanel=0
fi
if [ bRunOnPanel = 1 ]
then
    os=/mnt/flashNS4OS
    osnfs=/mnt/temp
else
	os=./flashNS4OS
	osnfs=./temp
fi
#destF=192.168.1.36:/home/ubuntu/WorkStation/SVN/NetAXSReleaseToTests/Release/NS4MIRO
destF=192.168.1.36:/home/ubuntu/WorkStation/SVN/AG016-NetAXS-4-PCA-VE/trunk/Code/LinuxSystem/NS4/releases2

if [ -z $1 ]
then
	help;
	quit 0
fi

echo -e "\n---------------------update NS4 panel to 368 kernel for MIRO issue----------------"
if [ "$1" = "clear" ] # if clear command then exit anyway here
then
	clear;
	quit 0;
fi

if [ -d $os ]
then
	clear;
	quit 0
fi 

echo "create folder $os , mount nfs folder $osnfs"
mkdir $os
mkdir $osnfs
echo "mount -t nfs -o nolock $destF  $osnfs"
mount -t nfs -o nolock $destF  $osnfs

echo "copy files...."
if [ "$1" != "clear" ] 
then
	for f in $osnfs/$1/NetAXS*;
	do
		if [ -d $f ]
		then
			osbinfolder=$f
			echo $f
		fi
	done
fi

if [ -z osbinfolder ]
then
	echo "------------Error------no find the destifolder "$1"-----------------"
quit 0
fi

#copy the upgrade files to desti folder
echo "source folder : $osbinfolder"
for f in $osbinfolder/*;
do
	echo "copy file $f-->$os"
	cp $f $os
done

# if this is a PC runnning the scripts , then stop here, no more upgrading
if [ bRunOnPanel = 0 ]
then
	echo "PC: no really need to run uprade, done here!"
	quit 0
fi

echo "upgrading NS4 panel..."
source $os/command
clear;
quit 0
